package com.xidian.zhiancloud.service.riskcontrol.riskpointslist;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.riskcontrol.TRiskLedgerStatusMapper;
import com.xidian.zhiancloud.dao.riskcontrol.riskidenva.*;
import com.xidian.zhiancloud.dao.riskcontrol.riskpointslist.TEquipFacStatusMapper;
import com.xidian.zhiancloud.model.riskcontrol.TEquipFacStatusList;
import com.xidian.zhiancloud.model.riskcontrol.TRiskLedgerStatusList;
import com.xidian.zhiancloud.model.riskcontrol.entity.TEquipFacStaEntity;
import com.xidian.zhiancloud.model.riskcontrol.entity.TEquipFacStaGY00Entity;
import com.xidian.zhiancloud.model.riskcontrol.entity.TEquipFacStaSS00Entity;
import com.xidian.zhiancloud.model.riskcontrol.entity.TEquipFacStaZY00Entity;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 风险管控-风险点清单
 */
@Service
public class TEquipFacStatusService extends ServiceImpl<TEquipFacStatusMapper,TEquipFacStatusList>{

    @Autowired
     TEquipFacStatusMapper tEquipFacStatusMapper;

    @Autowired
    TRiskCheckItemsMapper tRiskCheckItemsMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    TRiskAssessmentMeasuresMapper tRiskAssessmentMeasuresMapper;

    @Autowired
    TRiskValueLevelMapper tRiskValueLevelMapper;

    @Autowired
    TRiskLedgerStatusMapper tRiskLedgerStatusMapper;

    @Autowired
    TRiskJobCheckItemMapper tRiskJobCheckItemMapper;

    @Autowired
    TRiskProcessInspectionItemMapper tRiskProcessInspectionItemMapper;
    
    public Page<TEquipFacStatusList> getTEquipFacStatusList(
            BaseReqQuery reqdata, Integer equipType,String riskpointType,
            Long companyId,Integer status,Integer administerId){
        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        Page<TEquipFacStatusList> pagedata = tEquipFacStatusMapper.getTEquipFacStatusList
                (reqdata.getSearchKey(),equipType,riskpointType,companyId,
                    status,administerId);
        return pagedata;
    }

    public int insertTEquipFacStatusList(TEquipFacStatusList tefsdata){
        return tEquipFacStatusMapper.insertTEquipFacStatusList(tefsdata);
    }

    public int deleteTEquipFacStatus(String id,String ids){
        List<String> list = getList(id);
        List<String> lists = getList(ids);
        tEquipFacStatusMapper.deleteTEquipFacStatus(list);
        return tRiskCheckItemsMapper.deleteTRiskCheckItems(lists);
    }

    /**
     * id放入list
     * @param id id(多个已逗号分隔)
     * @return List集合
     */
    public List<String> getList(String id) {
        List<String> list = new ArrayList<String>();
        String[] str = id.split(",");
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }

    public int modifyTEquipFacStatus(Long id,TEquipFacStatusList tefsdata){
        return tEquipFacStatusMapper.modifyTEquipFacStatus(id,tefsdata);
    }

    public TEquipFacStatusList selectByCompanyIdAndEquipName(
            Long companyId,String riskpointType,String equipName){
        return tEquipFacStatusMapper.
                selectByCompanyIdAndEquipName(companyId,riskpointType, equipName);
    }

    public List<TEquipFacStatusList> getById(Long companyId,String riskpointsId){
        return tEquipFacStatusMapper.getById(companyId,riskpointsId);
    }

    public Page<TEquipFacStaEntity> getExportEquipFacStaData
            (Long companyId,String riskpointType,String id){
        List<Object> list = getObjeList(id);
        Page<TEquipFacStaEntity> pagedata = tEquipFacStatusMapper.
                getExportEquipFacStaData(companyId,riskpointType,list);
        return pagedata;
    }

    public List<Object> getObjeList(String ids) {
        List<Object> list = new ArrayList<Object>();
        String[] str = ids.split(",");
        long[] strArrNum = (long[]) ConvertUtils.convert(str,long.class);
        for (Long id : strArrNum) {
            list.add(id);
        }
        return list;
    }

    public void updateIdAndRiskPicture
            (String riskPointsId,String riskPicture){
        List<String> list = getList(riskPointsId);
        tEquipFacStatusMapper.updateIdAndRiskPicture(list,riskPicture);
    }

    @Transactional
    public int addSS00ExcelFile(Long companyId,Long userId,List<TEquipFacStaSS00Entity> ss00ExcelList){
        int res = tEquipFacStatusMapper.addSS00ExcelFile(companyId,userId,ss00ExcelList);
        
        List<String> idFacList = new ArrayList<>();
        if (ss00ExcelList.size()>0) {
            for (int i = 0; i < ss00ExcelList.size(); i++) {
                idFacList.add(ss00ExcelList.get(i).getChId());
            }
        }
        tRiskCheckItemsMapper.addSS00List(companyId, userId.toString(), ss00ExcelList);
        
        tRiskAssessmentMeasuresMapper.addSS00AsseList(companyId, userId.toString(), ss00ExcelList);

        tRiskValueLevelMapper.addSS00ValueLevel(companyId, userId.toString(), ss00ExcelList);
        
        List<TRiskLedgerStatusList> rLedList = new ArrayList<>();
        for (int i = 0; i < ss00ExcelList.size(); i++) {
            TRiskLedgerStatusList rLed = new TRiskLedgerStatusList();
            rLed.setCompanyId(companyId);
            rLed.setRiskPointType("ss00");
            rLed.setRiskPointsId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setCheckItemId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setLevelEval(null);
            rLed.setLevelRisk(null);
            rLed.setLevelProp(null);
            rLed.setRiskAssmeaId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setControlLevel(null);
            rLed.setLocale(ss00ExcelList.get(i).getPositionId());
            rLed.setEval(null);
            rLed.setRiskValue(ss00ExcelList.get(i).getOneExiRiskValue());
            rLed.setRiskColor(null);
            rLed.setTime(new Date());
            rLed.setUnit(null);
            rLed.setPersonLiable(ss00ExcelList.get(i).getAdministerId());
            rLed.setRiskSource("风险点导入");
            
            rLedList.add(rLed);
        }
        tRiskLedgerStatusMapper.insertLedgerStaList(rLedList);
        return res;
    }

    @Transactional
    public int addZY00ExcelFile(Long companyId,Long userId,List<TEquipFacStaZY00Entity> ss00ExcelList){
        int res = tEquipFacStatusMapper.addZY00ExcelFile(companyId,userId,ss00ExcelList);

        tRiskJobCheckItemMapper.addZY00List(companyId, userId.toString(), ss00ExcelList);

        tRiskAssessmentMeasuresMapper.addZY00AsseList(companyId, userId.toString(), ss00ExcelList);

        tRiskValueLevelMapper.addZY00ValueLevel(companyId, userId.toString(), ss00ExcelList);

        List<TRiskLedgerStatusList> rLedList = new ArrayList<>();
        for (int i = 0; i < ss00ExcelList.size(); i++) {
            TRiskLedgerStatusList rLed = new TRiskLedgerStatusList();
            rLed.setCompanyId(companyId);
            rLed.setRiskPointType("zy00");
            rLed.setRiskPointsId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setCheckItemId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setLevelEval(null);
            rLed.setLevelRisk(null);
            rLed.setLevelProp(null);
            rLed.setRiskAssmeaId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setControlLevel(null);
            rLed.setLocale(ss00ExcelList.get(i).getPositionId());
            rLed.setEval(null);
            rLed.setRiskValue(ss00ExcelList.get(i).getOneExiRiskValue());
            rLed.setRiskColor(null);
            rLed.setTime(new Date());
            rLed.setUnit(null);
            rLed.setPersonLiable(ss00ExcelList.get(i).getAdministerId());
            rLed.setRiskSource("风险点导入");

            rLedList.add(rLed);
        }
        tRiskLedgerStatusMapper.insertLedgerStaList(rLedList);
        return res;
    }

    @Transactional
    public int addGY00ExcelFile(Long companyId,Long userId,List<TEquipFacStaGY00Entity> ss00ExcelList){
        int res = tEquipFacStatusMapper.addGY00ExcelFile(companyId,userId,ss00ExcelList);

        List<String> idFacList = new ArrayList<>();
        if (ss00ExcelList.size()>0) {
            for (int i = 0; i < ss00ExcelList.size(); i++) {
                idFacList.add(ss00ExcelList.get(i).getChId());
            }
        }
        tRiskProcessInspectionItemMapper.addGY00List(companyId, userId.toString(), ss00ExcelList);

        tRiskAssessmentMeasuresMapper.addGY00AsseList(companyId, userId.toString(), ss00ExcelList);

        tRiskValueLevelMapper.addGY00ValueLevel(companyId, userId.toString(), ss00ExcelList);

        List<TRiskLedgerStatusList> rLedList = new ArrayList<>();
        for (int i = 0; i < ss00ExcelList.size(); i++) {
            TRiskLedgerStatusList rLed = new TRiskLedgerStatusList();
            rLed.setCompanyId(companyId);
            rLed.setRiskPointType("gy00");
            rLed.setRiskPointsId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setCheckItemId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setLevelEval(null);
            rLed.setLevelRisk(null);
            rLed.setLevelProp(null);
            rLed.setRiskAssmeaId(Integer.valueOf(ss00ExcelList.get(i).getId()));
            rLed.setControlLevel(null);
            rLed.setLocale(ss00ExcelList.get(i).getPositionId());
            rLed.setEval(null);
            rLed.setRiskValue(ss00ExcelList.get(i).getOneExiRiskValue());
            rLed.setRiskColor(null);
            rLed.setTime(new Date());
            rLed.setUnit(null);
            rLed.setPersonLiable(ss00ExcelList.get(i).getAdministerId());
            rLed.setRiskSource("风险点导入");

            rLedList.add(rLed);
        }
        tRiskLedgerStatusMapper.insertLedgerStaList(rLedList);
        return res;
    }
    
}
